package com.omybug.springboot.redis;

import com.omybug.springboot.redis.distributedlocker.DistributedLocker;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.client.RedisClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author oMyBug
 * @since 2019/6/11 9:44
 */
@RestController
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private RedissonClient redissonClient;

    @GetMapping
    public String index(Integer userId){
        RLock lock = redissonClient.getLock("redis:lock:" + userId);
        lock.lock(10, TimeUnit.SECONDS);
        try {
            System.out.println(userId + " start " + Thread.currentThread().getId() + " " + System.currentTimeMillis());
            Thread.sleep(5*1000);
            System.out.println(userId + " end " + Thread.currentThread().getId() + " " + System.currentTimeMillis());
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
        return "success";
    }

    @GetMapping("/rush")
    public String rush(Integer userId){
        RLock lock = redissonClient.getLock("redis:lock:" + userId);
        boolean hasLock = false;
        try {
            hasLock = lock.tryLock(0L,10L, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if(!hasLock){
            return "fail";
        }
        try {
            System.out.println(userId + " start " + Thread.currentThread().getId() + " " + System.currentTimeMillis());
            Thread.sleep(5*1000); // 模拟业务处理时间
            System.out.println(userId + " end " + Thread.currentThread().getId() + " " + System.currentTimeMillis());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(hasLock){
                lock.unlock();
            }
        }
        return "success";
    }

    @Autowired
    private DistributedLocker distributedLocker;

    @GetMapping("/rush2")
    public String rush2(Integer userId){
        String lockKey = "redis:lock:" + userId;
        boolean hasLock = distributedLocker.tryLock(lockKey,0L,10L, TimeUnit.SECONDS);
        if(!hasLock){
            return "fail";
        }
        try {
            System.out.println(userId + " start " + Thread.currentThread().getId() + " " + System.currentTimeMillis());
            Thread.sleep(5*1000); // 模拟业务处理时间
            System.out.println(userId + " end " + Thread.currentThread().getId() + " " + System.currentTimeMillis());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            distributedLocker.unlock(lockKey);
        }
        return "success";
    }


}
